# -*- coding:utf-8 -*-
import requests, time, datetime,re,os,random
from bs4 import BeautifulSoup as bs

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
}
# 获取频道节目列表
def get_epgs_tvmao2(channel,channel_id,dt,func_arg):
    epgs = []
    desc = ''
    msg = ''
    success = 1
    ban = 0 #标识是否被BAN掉了,此接口不确定是否有反爬机制
    now_date = datetime.datetime.now()
    need_date = dt
    delta = need_date - now_date
    now_weekday = now_date.weekday()
    need_weekday = now_weekday + delta.days + 1
    id_split = channel_id.split('-')
    if len(id_split) == 2:
        id = id_split[1]
    elif len(id_split) ==3:
        id = '-'.join(id_split[1:3])
    else:
        id = channel_id
    url = "https://lighttv.tvmao.com/qa/qachannelschedule?epgCode=%s&op=getProgramByChnid&epgName=&isNew=on&day=%s"%(id,need_weekday)
    # print(url)
    try:
        time.sleep(random.randint(5, 10)) # 防止被封,每次请求间隔10秒
        res = requests.get(url,headers = headers)
        res_j = res.json()
        datas = res_j[2]['pro']   # {'name': '星空放映厅', 'typeid': 0, 'pid': 'FpK3GaM', 'time': '00:15', 'status': -1}, 
        # print(type(datas))
        for data in datas:
            epgs.append(data) # {'name': '星空放映厅', 'typeid': 0, 'pid': 'FpK3GaM', 'time': '00:15', 'status': -1}
        # print(epgs)
        return {
            'success': success,
            'epgs': epgs,
            'msg': msg,
            'last_program_date': dt,
            'ban': ban,
            'source':'tvmao'
        }
    except Exception as e:
        success = 0
        msg = 'spider-tvmao-%s'%e
        ret = {
            'success': success,
            'epgs': epgs,
            'msg': msg,
            'last_program_date': dt,
            'ban': 0,
            'source':'tvmao'
        }
    return ret

# 获取频道列表
def get_channels_tvmao(sorts={'央视': 'cctv', '卫视': 'satellite','辽宁': 'LNTV','沈阳': 'LNSY'}):
    channels = []
    # url_sort = 'https://www.tvmao.com/program/playing/'
    # res = requests.get(url_sort,headers = headers,timeout = 5)
    # res.encoding = 'utf-8'
    # soup = bs(res.text,'html.parser')
    # provinces = {}
    # big_sorts = {}
    # provinces_more = soup.select('div.province > ul.province-list > li')
    # big_sorts_more = soup.select('dl.chntypetab > dd')
    # for province_more in provinces_more:
    #     province = province_more.text.strip().replace('黑龙','黑龙江')
    #     province_id = province_more.a['href'].replace('/program/playing/','').replace('/','')
    #     province = {
    #         province:province_id,
    #     }
    #     provinces.update(province)
    # for big_sort_more in big_sorts_more:
    #     sort_name = big_sort_more.text.strip()
    #     url = big_sort_more.a['href']
    #     sort_id = url.replace('/program/playing/','').replace('/','')
    #     # print(sort_name,sort_id)
    #     if sort_name in provinces or sort_name == '收藏' or sort_name == '数字付费':
    #         continue
    #     big_sorts.update({sort_name:sort_id})
    # provinces.update(big_sorts)
    # sorts = provinces

    n = 0
    for sort_name in sorts:
        time.sleep(random.randint(5, 10)) # 防止被封,每次请求间隔10秒以上
        url = 'https://www.tvmao.com/program/playing/%s'%sorts[sort_name]
        res = requests.get(url,headers = headers,timeout = 5)
        res.encoding = 'utf-8'
        soup = bs(res.text,'html.parser')
        channel_trs = soup.select('table.timetable > tr')
        n += 1
        
        for tr in channel_trs:
            xmls = []
            tr1 = tr.td.a
            name = tr1['title']
            # 如果name中包含有“频道”则替换为空
            name = name.replace('频道','')
            if name == "CCTV5加":
                name = "CCTV-5+体育赛事"
            name = name.upper().strip()
            name = name.replace(' ','')
            href = tr1['href']
            id = href.replace('/program/','').replace('/','-').replace('.html','').replace('-program_','')
            id = re.sub('-w\d$','',id)
            res1 = tr1['res']
            channel = {
                'name': name,
                'id': [id],
                'url': 'https://m.tvmao.com/program/%s.html' % id,
                'source': 'tvmao',
                'logo': '',
                'desc': '',
                'sort': sort_name,
                'res': res1,
            }
            channels.append(channel)
            xmls.append('<channel id="%s"><display-name lang="zh">%s</display-name></channel>' % (name, name))
            print('<channel id="%s"><display-name lang="zh">%s</display-name></channel>' % (name, name))
            dt = datetime.datetime.now()
            program = get_epgs_tvmao2(channel,id,dt,{})
            programs = program['epgs'] # {'name': '星空放映厅', 'typeid': 0, 'pid': 'FpK3GaM', 'time': '00:15', 'status': -1}
            # <programme start="20240807040000 +0800" stop="20240807041220 +0800" channel="1"><title lang="zh">2024秘境之眼-峭壁精灵黑叶猴(4K)</title><desc lang="zh"></desc></programme>
            for program in programs:
                id = channel['id']
                start = program['time'] # '00:15'
                dt_srr = re.sub(r'[:：]', '', start) 

                start = f"{datetime.datetime.now().strftime('%Y%m%d')}{dt_srr}00 +0800"
                # 结束时间等于下一条的开始时间
                try:
                    end = programs[programs.index(program)+1]['time']
                    end = re.sub(r'[:：]', '', end) 
                    end = f"{datetime.datetime.now().strftime('%Y%m%d')}{end}00 +0800"
                except:
                    end = f"{datetime.datetime.now().strftime('%Y%m%d')}235900 +0800"
                title = program['name']
                xmls.append('<programme start="%s" stop="%s" channel="%s"><title lang="zh">%s</title><desc lang="zh"></desc></programme>' % (start, end, name, title))
                print('<programme start="%s" stop="%s" channel="%s"><title lang="zh">%s</title><desc lang="zh"></desc></programme>' % (start, end, name, title))

            # 获取当前日期
            dtx = datetime.datetime.now().strftime('%Y%m%d')
            path = os.path.join(os.path.dirname(os.path.abspath(__file__)), f'channel/tvmao_{dtx}.xml')

            # 读取原文件内容，避免使用'a'模式
            with open(path, 'r', encoding='utf-8') as f1:
                lines = f1.readlines()

            # 删除原有最后一行</tv>
            if lines and lines[-1].strip() == '</tv>':
                lines = lines[:-1]

            # 写入新的内容
            with open(path, 'w', encoding='utf-8') as f:
                f.writelines(lines)
                f.write('\n'.join(xmls) + '\n')
                f.write('</tv>')
            
    return channels

def main():
    # 保存到当前路径下的channel文件夹中，文件名为tvsou_20240807.xml
    dt = datetime.datetime.now().strftime('%Y%m%d')
    path = os.path.dirname(os.path.abspath(__file__))+f'/channel/tvmao_{dt}.xml'
    # 保存xml格式
    with open(path, 'w', encoding='utf-8') as f:
        heade_str='''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "http://nas.laoknas.com:8181/epg/get_epg_xml.dtd">
<tv>\n'''
        heade_str = heade_str +'</tv>'
        f.write(heade_str)
        f.close()
    get_channels_tvmao()
if __name__ == '__main__':
    sorts = {
        '央视': 'cctv', 
        '卫视': 'satellite',
        '辽宁': '210000',
        '吉林': '220000',
        '北京': '110000', 
        '黑龙江': '230000', 
        '上海': '310000', 
        '天津': '120000', 
        '江苏': '320000',
        '江西': '360000', 
        '广东': '440000', 
        '贵州': '520000', 
        '宁夏': '640000', 
        '河北': '130000', 
        '浙江': '330000', 
        '山东': '370000', 
        '河南': '410000', 
        '广西': '450000', 
        '云南': '530000', 
        '陕西': '610000', 
        '内蒙古': '150000',
        '新疆': '650000', '山西': '140000', '安徽': '340000', '湖北': '420000', '海南': '460000', '重庆': '500000', '西藏': '540000', '甘肃': '620000', '内蒙': '150000', '福建': '350000', '湖南': '430000', '四川': '510000', '青海': '630000'}
    # get_channels_tvmao()
    main()